/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.jrwz.server.datatables;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import l1j.jrwz.server.model.L1Object;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.model.npc.action.L1NpcAction;
import l1j.jrwz.server.model.npc.action.L1NpcXmlParser;
import l1j.jrwz.server.utils.FileUtil;
import l1j.jrwz.server.utils.PerformanceTimer;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class NpcActionTable {
    private static Logger _log = Logger.getLogger(NpcActionTable.class
            .getName());
    private static NpcActionTable _instance;

    public static NpcActionTable getInstance() {
        return _instance;
    }

    public static void load() {
        try {
            PerformanceTimer timer = new PerformanceTimer();
            System.out.print("loading npcaction...");
            _instance = new NpcActionTable();
            System.out.println("OK! " + timer.get() + "ms");
        } catch (Exception e) {
            _log.log(Level.SEVERE, "NpcAction无法加载。", e);
            System.exit(0);
        }
    }

    private final List<L1NpcAction> _actions = new ArrayList<L1NpcAction>();

    private final List<L1NpcAction> _talkActions = new ArrayList<L1NpcAction>();

    private NpcActionTable() throws Exception {
        File usersDir = new File("./data/xml/NpcActions/users/");
        if (usersDir.exists()) {
            loadDirectoryActions(usersDir);
        }
        loadDirectoryActions(new File("./data/xml/NpcActions/"));
    }

    public L1NpcAction get(L1PcInstance pc, L1Object obj) {
        for (L1NpcAction action : _talkActions) {
            if (action.acceptsRequest("", pc, obj)) {
                return action;
            }
        }
        return null;
    }

    public L1NpcAction get(String actionName, L1PcInstance pc, L1Object obj) {
        for (L1NpcAction action : _actions) {
            if (action.acceptsRequest(actionName, pc, obj)) {
                return action;
            }
        }
        return null;
    }

    private void loadAction(File file) throws Exception {
        _actions.addAll(loadAction(file, "NpcActionList"));
    }

    private List<L1NpcAction> loadAction(File file, String nodeName)

    throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document doc = builder.parse(file);

        if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(nodeName)) {
            return new ArrayList<L1NpcAction>();
        }
        return L1NpcXmlParser.listActions(doc.getDocumentElement());
    }

    private void loadDirectoryActions(File dir) throws Exception {
        for (String file : dir.list()) {
            File f = new File(dir, file);
            if (FileUtil.getExtension(f).equalsIgnoreCase("xml")) {
                loadAction(f);
                loadTalkAction(f);
            }
        }
    }

    private void loadTalkAction(File file) throws Exception {
        _talkActions.addAll(loadAction(file, "NpcTalkActionList"));
    }
}
